/**
 * 合并有序数组
 * @param {String} arr1 
 * @param {String} arr2 
 */
function concatArray(arr1, arr2) {
    arr1 = arr1.split(',');
    arr2 = arr2.split(',');

    if (arr1.length < arr2.length) {
        let temp = arr1;
        arr1 = arr2;
        arr2 = temp;
    }
    let stack = []; //返回的队列
    let j = 0;
    console.log(arr1, arr2);
    //合并
    arr1.forEach(e => {
        // console.log(`e:${e}`, arr1.indexOf(e));
        let i = j; //第二个数组的下标
        while (i < arr2.length) {
            console.log(i, j, arr2[i], e);
            if (arr2[i] > e) {
                break;
            }
            stack.push(arr2[i]);
            i++;
            j = i;
        }
        stack.push(e);
    });
    return stack.join(',');
}


/**
 * 合并有序数组
 * 指针方法
 */
function concatArray2(arr1, arr2) {

    arr1 = arr1.split(',');
    arr2 = arr2.split(',');
    let pointer = 0; //指针
    let result = []; //结果数组

    for (let i = 0; i < arr1.length; i++) {
        const element = arr1[i];
        if (element >= arr2[pointer]) {
            // console.log('111', pointer);
            //第二个参数为0,则不会删除数据
            result.push(arr2[pointer]);
            pointer++;
        }
        result.push(element);
    }
    if (pointer < arr2.length) {
        result.push(...arr2.splice(pointer, arr2.length - pointer));
    }
    return result;
}

console.log(concatArray2('1,2,4', '1,3,4,5'));
console.log(concatArray2('1,3,4,5', '1,2,4'));